package gui.del_object;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bdd_handler.PersonneDAO;
import bdd_handler.bdd;

import objects.Personne;

public class Del_Connaissance extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JFrame parent;
	private Personne p1;
	private Personne p2;
	private PersonneDAO pDAO;
	private JButton valider;
	private JButton annuler;

	public Del_Connaissance(JFrame parent, Personne p1, Personne p2)
	{
		//recuperation de la fenetre parente
				this.parent=parent;
				this.parent.setVisible(false);

				//construction de la fenetre
				this.p1=p1;
				this.p2=p2;
				this.setTitle("Suppression de la connaissance : "+p2.getNom()+" "+p2.getPrenom());
				this.setSize(1020,650);
				this.setResizable(false);
				JPanel pan=new JPanel();
				pan.setLayout(null);
				this.setContentPane(pan);
				
				//creation des DAOs
				bdd db=new bdd();
				pDAO=new PersonneDAO(db.getConnection());
				
				//creation objets internes
				JLabel avert=new JLabel("Attention! Vous allez supprimer la connaissance '"+p2.getNom()+" "+p2.getPrenom()+"'. Cette action est irréversible.");
				valider=new JButton("supprimer");
				valider.addActionListener(this);
				annuler=new JButton("annuler");
				annuler.addActionListener(this);
				//placer les objets
				pan.add(avert);
				avert.setBounds(10,10,900,20);
				pan.add(annuler);
				annuler.setBounds(10, 40, 110, 20);
				pan.add(valider);
				valider.setBounds(130, 40, 110, 20);

				//action fermeture fenetre
				this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
				this.addWindowListener(
						new WindowAdapter(){
							public void windowClosing(WindowEvent e){
								close();

							}
						}
						);
				//afficher la fenetre
				this.setVisible(true);
			}
	
	private void close(){
		this.parent.setVisible(true);
		this.dispose();
	}

	public void actionPerformed(ActionEvent e) {

		Object source=e.getSource();

		if(source==valider)
		{
			pDAO.delConnaissance(p1, p2);
			close();
		}
		if(source==annuler)
		{
			close();
		}
	}
}